package com.study.edoc.service.impl;


import com.google.common.collect.Maps;
import com.study.edoc.dao.UserInfoMapper;
import com.study.edoc.exception.BusinessException;
import com.study.edoc.pojo.UserInfo;
import com.study.edoc.service.UserService;
import com.study.edoc.utils.PasswordEncoder;
import com.study.edoc.utils.PasswordUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Resource
    private UserInfoMapper userInfoMapper;


    @Override
    public int deleteById(Integer id) {
        return userInfoMapper.deleteById(id);
    }

    @Override
    public int addUser(UserInfo userInfo) {
        UserInfo user = null;
        Map<String,Object> paramMap = Maps.newHashMap();
        Class<? extends UserInfo> userCls = userInfo.getClass();
        Field[] fields = userCls.getDeclaredFields();
        String key = "";
        for (int i = 0; i < fields.length; i++) {
            fields[i].setAccessible(true);
            System.out.println(">>>>>>>>>>>>>" + fields[i].getName());
            if(fields[i].getName().equalsIgnoreCase("email")){
                key = "email";
                paramMap.put("key",key);
                paramMap.put("value",userInfo.getEmail());
                user = userInfoMapper.selectByMap(paramMap);
                if(user != null){
                    throw new BusinessException("电子邮箱地址已存在。");
                }
                break;
            }
            else if(fields[i].getName().equalsIgnoreCase("phone")){
                key = "phone";
                paramMap.put("key",key);
                paramMap.put("value",userInfo.getPhone());
                user = userInfoMapper.selectByMap(paramMap);
                if(user != null){
                    throw new BusinessException("手机号已被注册。");
                }
                break;
            }
            else if(fields[i].getName().equalsIgnoreCase("username")){
                key = "username";
                paramMap.put("key",key);
                paramMap.put("value",userInfo.getUsername());
                user = userInfoMapper.selectByMap(paramMap);
                if(user != null){
                    throw new BusinessException("用户名已被使用，请修改后再试。");
                }
                break;
            }
        }

        if(Objects.isNull(user)) {
            return userInfoMapper.insert(userInfo);
        }
        return 0;
    }

    @Override
    public int smartSaveUser(UserInfo userInfo) {
        return userInfoMapper.dynamicUpdate(userInfo);
    }

    @Override
    public UserInfo getUserById(Integer id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public int smartUpdateUser(UserInfo userInfo) {
        return userInfoMapper.dynamicUpdate(userInfo);
    }

    @Override
    public int updateUser(UserInfo userInfo) {
        return userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo login(String phone, String password) {
        UserInfo userInfo = null;
        Map<String,Object> paramMap = Maps.newHashMap();
        paramMap.put("key","phone");
        paramMap.put("value",phone);
        userInfo = userInfoMapper.selectByMap(paramMap);
        if(userInfo != null){
            if(userInfo.getPassword().equals(password) && !userInfo.getStatus()){
                return userInfo;
            }
        }
        return null;
    }

    @Override
    public boolean register(UserInfo userInfo) {
        userInfo.setStatus(false);
        userInfo.setDeleted(false);
        userInfo.setCreateTime(new Date());
        userInfo.setSalt(PasswordUtils.getSalt());
        userInfo.setPassword(PasswordUtils.encode(userInfo.getPassword(),userInfo.getSalt()));
        return addUser(userInfo) > 0;
    }
}
